import Vue from 'vue'
import Vuex from 'vuex'

import api from "../http/api";
import http from "../http/http";
import "../http/axios";

import VueCookie from 'vue-cookie'
Vue.use(VueCookie)

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    //临时存储课程信息
    temp_courseSections: window.localStorage.getItem('temp_courseSections') == null ? '' : JSON.parse(window.localStorage.getItem('temp_courseSections' || '[]')),
    course_title: window.localStorage.getItem('course_title') == null ? '' : JSON.parse(window.localStorage.getItem('course_title' || '[]')),
    identifier: window.localStorage.getItem('identifier') == null ? '' : JSON.parse(window.localStorage.getItem('identifier' || '[]')),
    // 旧方法，利用localStorage存储数据
    // id: window.localStorage.getItem('id') == null ? '' : JSON.parse(window.localStorage.getItem('id' || '[]')),
    // name: window.localStorage.getItem('name') == null ? '未登录' : JSON.parse(window.localStorage.getItem('name' || '[]')),
    // roleName: window.localStorage.getItem('roleName') == null ? '' : JSON.parse(window.localStorage.getItem('roleName' || '[]')),

    //新方法利用cookie存储数据
    id: Vue.cookie.get('id') == null ? '' : Vue.cookie.get('id'),
    name: Vue.cookie.get('name') == null ? '未登录' : Vue.cookie.get('name'),
    roleName: Vue.cookie.get('roleName') == null ? '' : Vue.cookie.get('roleName'),
  },
  mutations: {
    saveSections(state, data) {
      state.temp_courseSections = data.sections
      state.course_title = data.title
      state.identifier = data.identifier
      window.localStorage.setItem('temp_courseSections', JSON.stringify(state.temp_courseSections))
      window.localStorage.setItem('course_title', JSON.stringify(state.course_title))
      window.localStorage.setItem('identifier', JSON.stringify(state.identifier))
    },
    login(state, data) {
      state.id = data.id
      state.name = data.name
      state.roleName = data.roleName
      //旧方法
      // window.localStorage.setItem('id', JSON.stringify(state.id))
      // window.localStorage.setItem('name', JSON.stringify(state.name))
      // window.localStorage.setItem('roleName', JSON.stringify(state.roleName))

      //新方法
      Vue.cookie.set('id', state.id, { expires: '20m' });
      Vue.cookie.set('name', state.name, { expires: '20m' });
      Vue.cookie.set('roleName', state.roleName, { expires: '20m' });
    },
    logout(state) {
      state.id = ''
      state.name = '未登录'
      state.roleName = ''
      //旧方法
      // window.localStorage.removeItem('id')
      // window.localStorage.removeItem('name')
      // window.localStorage.removeItem('roleName')

      http.get(api.Logout).then((resp) => {
        if (resp.success) {
          // 注意不能用 null 清除，否则将无法判断 user 里具体的内容
          console.log('后端用户注销成功')
        }
        else {
          console.log("'后端用户注销失败")
        }
      });

      // 新方法
      Vue.cookie.delete('id')
      Vue.cookie.delete('name')
      Vue.cookie.delete('roleName')

      Vue.cookie.delete('JSESSIONID')
    }
  },
  actions: {
  },
  modules: {
  }
})
